package org.jl.main;

import org.jl.main.util.UtilForCollection;

import java.util.*;
import java.util.concurrent.*;

/**
 * java集合
 */
public class Main02_1_Collection {

    /*
    java集合-集
    运营结果：
    set1 is empty:true
    set1 is empty:true

    set1 = []
    set1 = [null]

    set1 = [a2, a3, a1]
    set2 = [a1, a2, a3]
    set3 = [a2, a3, a1]

    cSet1 = [a1, a2, a3]
     */
    public static void main1(String[] args) {
        //空集，返回默认值，不能存储数据
        Set<String> set1 = null;
        System.out.println("set1 is empty:" + UtilForCollection.isEmpty(set1));
        set1 = Collections.emptySet();
        System.out.println("set1 is empty:" + UtilForCollection.isEmpty(set1) + "\n");

        //初始容量16，不够以2的倍数增加
        set1 = new HashSet<>(64);
        System.out.println("set1 = " + set1);
        //支持空键
        set1.add(null);
        System.out.println("set1 = " + set1 + "\n");

        //无序集
        set1 = Set.of("a3", "a2", "a1");
        System.out.println("set1 = " + set1);
        //有序集，按键排序
        Set<String> set2 = new TreeSet<>();
        UtilForCollection.of(set2, "a3", "a2", "a1");
        System.out.println("set2 = " + set2);
        //有序集，按新增顺序排序
        Set<String> set3 = new LinkedHashSet<>();
        UtilForCollection.of(set3, "a2", "a3", "a1");
        System.out.println("set3 = " + set3 + "\n");

        //线程安全有序集，不支持空键，同TreeSet
        ConcurrentSkipListSet<String> cSet1 = new ConcurrentSkipListSet<>();
        UtilForCollection.of(cSet1, "a2", "a3", "a1");
        System.out.println("cSet1 = " + cSet1 + "\n");
    }

    /*
    java集合-映射
    运营结果：
    map1 is empty:true
    map1 is empty:true

    map1 = {}
    map1 = {null=null}

    map1 = {a3=3, a2=2, a1=1}
    map2 = {a1=1, a2=2, a3=3}
    map3 = {a2=2, a3=3, a1=1}

    cMap1 = {a1=1, a2=2, a3=3}
    cMap2 = {a1=1, a2=2, a3=3}
     */
    public static void main2(String[] args) {
        //空映射，返回默认值，不能存储数据
        Map<String, String> map1 = null;
        System.out.println("map1 is empty:" + UtilForCollection.isEmpty(map1));
        map1 = Collections.emptyMap();
        System.out.println("map1 is empty:" + UtilForCollection.isEmpty(map1) + "\n");

        //初始容量16，不够以2的倍数增加
        map1 = new HashMap<>(64);
        System.out.println("map1 = " + map1);
        //支持空键空值
        map1.put(null, null);
        System.out.println("map1 = " + map1 + "\n");

        //无序映射
        map1 = Map.of("a3", "3", "a2", "2", "a1", "1");
        System.out.println("map1 = " + map1);
        //有序映射，按照键的顺序排列，不支持空键
        Map<String, String> map2 = new TreeMap<>();
        UtilForCollection.of(map2, "a3", "3", "a2", "2", "a1", "1");
        System.out.println("map2 = " + map2);
        //有序映射，按照新增的顺序排列，不支持空键
        Map<String, String> map3 = new LinkedHashMap<>();
        UtilForCollection.of(map3, "a2", "2", "a3", "3", "a1", "1");
        System.out.println("map3 = " + map3 + "\n");

        //线程安全无序映射，不支持空键空值，不保证有序
        ConcurrentMap<String, String> cMap1 = new ConcurrentHashMap<>();
        cMap1.put("a3", "3");
        cMap1.put("a2", "2");
        cMap1.put("a1", "1");
        System.out.println("cMap1 = " + cMap1);
        //线程安全有序映射，不支持空键空值，同TreeMap
        ConcurrentMap<String, String> cMap2 = new ConcurrentSkipListMap<>();
        UtilForCollection.of(cMap2, "a3", "3", "a2", "2", "a1", "1");
        System.out.println("cMap2 = " + cMap2 + "\n");

        //遍历映射
        for (String key : map1.keySet()) {
            System.out.print("  key = " + key);
        }
        System.out.println();
        for (String value : map1.values()) {
            System.out.print("  value = " + value);
        }
        System.out.println("\n");

        int a = 1;
        for (Map.Entry<String, String> entry : map1.entrySet()) {
            System.out.print("  key = " + entry.getKey());
            System.out.println("  value = " + entry.getValue());
            a = 2;
        }
        System.out.println("a = " + a);
        System.out.println();

        //这种方式不能读取外部变量
        map1.forEach((key, value) -> {
            System.out.print("  key = " + key);
            System.out.println("  value = " + value);
//            a=2;
        });
    }

    /*
    java集合-列表
    运营结果：
    list1 is empty:true
    list1 is empty:true

    list1 = []
    list1 = [null]

    list1 = [a2, a3, a1]

    cList1 = [a2, a3, a1]
     */
    public static void main3(String[] args) {
        //空列表，返回默认值，不能存储数据
        List<String> list1 = null;
        System.out.println("list1 is empty:" + UtilForCollection.isEmpty(list1));
        list1 = Collections.emptyList();
        System.out.println("list1 is empty:" + UtilForCollection.isEmpty(list1) + "\n");

        //列表，查询效率高，LinkedList新增删除效率高
        list1 = new ArrayList<>();
        System.out.println("list1 = " + list1);
        //支持空值
        list1.add(null);
        System.out.println("list1 = " + list1 + "\n");

        list1 = List.of("a2", "a3", "a1");
        System.out.println("list1 = " + list1 + "\n");

        //线程安全，解决ArrayList在多线程中报ConcurrentModificationException问题
        List<String> cList1 = new CopyOnWriteArrayList<>();
        UtilForCollection.of(cList1, "a2", "a3", "a1");
        System.out.println("cList1 = " + cList1 + "\n");
    }

}
